import cfg from '@/common/config.js'
import f from '@/common/lib/f.js'
import { nav,isnull,storage } from './f';

const res = {
    ok: 0,
    msg: '异常错误'
};

console.log(storage('userToken'));

export function req(url,data,method,type,loading) {
    return new Promise((resolve,reject) => {
        type = type || 'form';
        type = type == 'form' ? 'application/x-www-form-urlencoded' : 'application/json';
        var herder = { 'Content-Type': type,'tenant-id': '1','Authorization': storage('userToken') || '' };
        // var herder = { 'Content-Type': type,'tenant-id': '1','Authorization': 'test311' };
        console.log(herder);

        method = method || 'GET'
        var urls = url; if (url.substr(0,4) != 'http') urls = cfg.url + url;
        loading = loading == undefined ? true : loading;
        loading && uni.showLoading({ mask: true });
        uni.request({
            header: herder,
            url: urls,
            method: method,
            data: data,
            dataType: 'json',
        }).then(response => {
            console.log(response);

            if (response[1].data.code == 401) {
                nav('/pages/login/login1','R') //跳转登录
            } else if (response[1].data.code == 1016001000) {
                nav('/subPackage1/basics/basics','R')
            } else if (response[1].data.code == 1016001004) {
                nav('/subPackage1/marriageMember/marriageMember','R')
            } else if (response[1].data.code == 0) {
                loading && uni.hideLoading();
                resolve(response[1].data)
            }
            else {
                uni.showToast({
                    title: response[1].data.msg,
                    icon: 'none',  // 没有图标
                    duration: 2000  // 显示时间
                });
                // uni.toa
                // f.msg('异常错误,请联系管理员')
            }
        }).catch(err => {
            loading && uni.hideLoading();
            console.error('请求失败:', err);
            reject(err);
        })
    });
}

// export function req(url, data, method){
//     return new Promise((resolve, reject) => {
//         method = method || 'GET'
//         var urls=url
//         if (url.substr(0,4)!='http') urls=cfg.url + url
//         uni.request({
//             url: urls,
//             // url: 'https://ydd.tysso.cn/mall/api/index.php/core/map/reverse_geocoding?lng=112.94547319535&lat=28.234889399944',
//             method,
//             data: data,
//             dataType: 'json',
//         }).then(response => {
//             resolve(response[1].data)
//         }).catch(err => {
//             resolve(res)

//         })
//     });
// }

export function req_test(loading) {
    return new Promise((resolve,reject) => {
        // loading=loading||true;
        // loading&&Toast.loading({ message: '加载中...', forbidClick: true, loadingType: 'spinner' });
        setTimeout(() => {
            resolve(res);
            // loading&&Toast.clear();
        },500);
    })
}